package com.pth.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.pth.dao.PxdataDAO;
import com.pth.model.Pxdata;

public class PxdataDAOImpl implements PxdataDAO {

	private HibernateTemplate hibernateTemplate;
	
	public void delete(Pxdata pxdata) {

		hibernateTemplate.delete(pxdata);
		
	}

	public int getAllNum() {
		
		return hibernateTemplate.find("from Pxdata").size();
		
	}

	public Pxdata getPxdataByid(int id) {

		return hibernateTemplate.get(Pxdata.class, id);
		
	}

	@SuppressWarnings("unchecked")
	public List<Pxdata> getPxdatas(final int pagesize,final int offset) {

		final String hqlString="from Pxdata p order by p.publishtime desc";
		
		List<Pxdata> list=hibernateTemplate.executeFind(new HibernateCallback() {

			public Object doInHibernate(Session arg0) throws HibernateException,
					SQLException {

				return arg0.createQuery(hqlString).setFirstResult(offset).setMaxResults(pagesize).list();
				
			}
		});
		
		return list;
	
	
	}

	public void save(Pxdata pxdata) {

		hibernateTemplate.save(pxdata);
	}

	public void update(Pxdata pxdata) {

		hibernateTemplate.update(pxdata);
		
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@SuppressWarnings("unchecked")
	public List<Pxdata> getPxdatasOrderByscannum(final int offset,final int pagesize) {
		
		final String hqlString="from Pxdata p order by p.scannum desc";
		
		List<Pxdata> list=hibernateTemplate.executeFind(new HibernateCallback() {

			public Object doInHibernate(Session arg0) throws HibernateException,
					SQLException {

				return arg0.createQuery(hqlString).setFirstResult(offset).setMaxResults(pagesize).list();
				
			}
		});
		
		return list;
	
	}

}
